package Calculator;

import Calculator.Exceptions.DivisionByZeroException;
import Calculator.Exceptions.UnclosedParenthesisException;
import Calculator.Exceptions.WrongExpressionException;
import Calculator.Exceptions.WrongSymbolException;
import Calculator.Parser.InfixParser;
import Calculator.Parser.RPNInterpreter;

public class Calculator {

	/**
	 * Evalúa la expresión matemática
	 * @param input Expresión matemática en forma de String
	 * @return Resultado de la función 
	 */
	public Double evaluate(String input) {
		String RPNString;
		try {
			InfixParser infixParser = new InfixParser(); 
			RPNString = infixParser.parseToReversePolishNotation(input);
			RPNInterpreter interpreter = new RPNInterpreter();
			Double result = interpreter.solve(RPNString);
			return result;
		} catch (WrongSymbolException e) {
			System.out.println(e.getMessage());
		} catch (UnclosedParenthesisException e) {
			System.out.println(e.getMessage());
		} catch (DivisionByZeroException e){
			System.out.println(e.getMessage());
		} catch (WrongExpressionException e){
			System.out.println(e.getMessage());
		}
		return null;
	}

}
